package org.jxf.gps.jfinal.model;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jfk.core.utils.GeoPoint;
import org.jfk.core.utils.GeoUtils;

public class FenceCircle extends Fence implements IFenceAssert{

	@Override
	public boolean isIn(GeoPoint p) {
		return GeoUtils.isInCircle(p.getLatitude(), p.getLongitude(), this.center[0], this.center[1], this.radius);
	}

	public boolean isOut(GeoPoint p) {
		return !GeoUtils.isInCircle(p.getLatitude(), p.getLongitude(), this.center[0], this.center[1], this.radius);
	}
	
	@Override
	public void loadDetail(String fenceDetail) {
		JSONObject j = JSONObject.fromObject(fenceDetail);
		this.setType(j.getString("type"));
		JSONObject dl = j.getJSONObject("detail");
		radius = dl.getDouble("radius");
		JSONArray c = dl.getJSONArray("center");
		center = new double[2];
		center[0] = c.getDouble(0);
		center[1] = c.getDouble(1);
	}
	
	@Override
	public boolean isOn(GeoPoint p) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isMoveIn(GeoPoint curr, GeoPoint last) {
		return isIn(curr) && isOut(last);
	}

	@Override
	public boolean isMoveOut(GeoPoint curr, GeoPoint last) {
		return isIn(last) && isOut(curr);
	}	

	private double[] center;
	private double radius;
	
	public double[] getCenter() {
		return center;
	}
	public void setCenter(double[] center) {
		this.center = center;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}


	public static void main(String[] args){
		FenceCircle f = new FenceCircle();
		f.loadDetail("{\"type\":\"Circle\",\"detail\":{\"center\":[22.82207380354523,113.87775421142578],\"radius\":5762.32343308803}}");
		GeoPoint p = new GeoPoint();
		p.setLatitude(22.92307380354523);
		p.setLongitude(113.87775421142578);
		System.out.println(f.isIn(p));
	}

}
